//
//  ShowQRcodeView.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/28.
//  Copyright © 2020 tym. All rights reserved.
//

#import "ShowAlertView.h"
#import "SGQRCodeObtain.h"
#import "UIView+ZFFrame.h"
#import "NSString+Option.h"
#import "Sizes.h"
#import "UIButton+Gradient.h"
#import "UserTool.h"
#import "UserModel.h"
@interface ShowAlertView()
@property(nonatomic,weak) IBOutlet UIView *conView;

@property(nonatomic,weak) IBOutlet UIButton *botCancelBtn;

@end

@implementation ShowAlertView

-(void)awakeFromNib{
    [super awakeFromNib];

    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.conView.layer.cornerRadius = 10;
    
    self.cancelBtn.layer.cornerRadius = 5;
    self.cancelBtn.layer.borderColor = rgba(155, 155, 155).CGColor;
    self.cancelBtn.layer.borderWidth = 1;
    
    self.sureBtn.layer.cornerRadius = 5;
    self.sureBtn.layer.masksToBounds = YES;
    [self.sureBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    
}

-(void)layoutSubviews{
    
}

-(IBAction)cancelAction:(id)sender{
    if (self.showAlertActionBlock) {
        self.showAlertActionBlock(0);
    }
    [self removeFromSuperview];
}

-(IBAction)sureAction:(id)sender{
    [self removeFromSuperview];
    
    if (self.showAlertActionBlock) {
        self.showAlertActionBlock(1);
    }
}

-(IBAction)bottomCancelAction:(id)sender{
    [self removeFromSuperview];
}

-(void)dealloc{
    NSLog(@"提示框释放");
}

+(void)showAlertViewTitle:(NSString*)title message:(NSString*)message actionBtnTitle:(NSString*)btnTitle
hiddenCancel:(BOOL)hidden
               completion:(void(^)(NSInteger actionType))completion{
    
    ShowAlertView *view = [[[NSBundle mainBundle] loadNibNamed:@"ShowAlertView" owner:self options:nil] lastObject];
    view.showAlertActionBlock = completion;
    view.titleLab.text = title;
    
    view.botCancelBtn.hidden = YES;
    view.cancelBtn.hidden = hidden;
    view.messageLab.text = message;
    [view.sureBtn setTitle:btnTitle forState:UIControlStateNormal];
    UIWindow *keyWindow = [[[UIApplication sharedApplication] windows] firstObject];
    view.frame = keyWindow.bounds;
    
    for (UIView *view in  keyWindow.subviews) {
        if ([view isKindOfClass:[ShowAlertView class]]) {
            [view removeFromSuperview];
        }
    }
    [keyWindow addSubview:view];
    
    
    if ([view.messageLab.text isEqualToString:@"确定删除并退出该家庭吗?"]) {
        view.seconds = 4;
        [view changeSecend];
    }
    
    if([view.messageLab.text isEqualToString:@"是否要同时删除该用户名下所有的开锁方式"]){
        view.botCancelBtn.hidden = NO;
        [view.cancelBtn setTitle:@"否" forState:UIControlStateNormal];
    }
}

-(void)changeSecend{
    if (_seconds > 0) {
        self.sureBtn.userInteractionEnabled = NO;
        _seconds =_seconds-1;
        [self.sureBtn setTitle:[NSString stringWithFormat:@"确定(%lds)",_seconds] forState:UIControlStateNormal];
        [self performSelector:@selector(changeSecend) withObject:self afterDelay:1];
        [self.sureBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(230, 230, 230),(id)rgba(230, 230, 230)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    } else{
         self.sureBtn.userInteractionEnabled = YES;
        [self.sureBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
        [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        _seconds = 3;
    }
}


@end
